#!/usr/local/python/bin/python
# coding=utf-8
__author__ = 'cz'

import os
# 文件处理工具类
import linecache


class FileUtil():
    # 判断文件是否存在
    @staticmethod
    def fileExists(filename):
        return os.path.isfile(filename)

    # 文件是否为空
    @staticmethod
    def isFileEmpty(filePath):
        try:
            content = open(filePath).read()
            if content.strip() == '':
                return True
            else:
                return False
        except Exception as e:
            print(e)

    # 获取文件大小
    @staticmethod
    def fileSize(filename):
        return os.path.getsize(filename)

    # 获取指定行内容(小于1G时更快)
    @staticmethod
    def getLineContent(filename, linenum):
        if linenum < 1:
            return ''
        # for currline, line in enumerate(open(filename, 'rb')):  # 二进制方法读取
        for currline, line in enumerate(open(filename, 'rU')):  # 一行中有\r也会算换行符
            if currline == linenum - 1:
                return line
        return ''

    # 获取指定行内容(大于1G时更快)
    @staticmethod
    def getTheLineContent(filename, linenum):
        global logger
        try:
            linecache.checkcache(filename)  # 检查缓存
            targetLine = linecache.getline(filename, linenum)  # 读取指定行内容
            # linecache.clearcache() # 释放缓存
            return targetLine
        except IOError as err:
            print("获取指定行内容异常" + str(err))

    # 释放缓存
    @staticmethod
    def clearcache():
        linecache.clearcache()  # 释放缓存

    # 获取文件总行数(二进制方法读取)
    @staticmethod
    def getFileLineNums(filename):
        count = 0
        buffer = ''
        lastbuffer = ''
        file = open(filename, 'rb')
        while True:
            buffer = file.read(8192 * 1024)
            if not buffer:
                break
            count += buffer.count('\n')
            lastbuffer = buffer
        file.close()
        if count > 0:
            lastLine = buffer[lastbuffer.rindex('\n') + 1:]
            # 判断最后一行是空的行
            if len(lastLine) > 0:
                return count + 1
        else:
            if len(lastbuffer) > 0:
                return 1
        return count

    # 获取文件总行数(一行中有\r也会算换行符)
    @staticmethod
    def getFileLineNums2(filename):
        count = 0
        buffer = ''
        lastbuffer = ''
        file = open(filename, 'rU')
        while True:
            buffer = file.read(8192 * 1024)
            if not buffer:
                break
            count += buffer.count('\n')
            lastbuffer = buffer
        file.close()
        if count > 0:
            lastLine = buffer[lastbuffer.rindex('\n') + 1:]
            # 判断最后一行是空的行
            if len(lastLine) > 0:
                return count + 1
        else:
            if len(lastbuffer) > 0:
                return 1
        return count

    # 文件删除处理
    @staticmethod
    def delFile(filename):
        os.remove(filename)

    # 替换\r字符并重新输出到新文件
    @staticmethod
    def tranFile(srcfilename, tranfilename):
        fp = open(tranfilename, 'w')
        for currline, line in enumerate(open(srcfilename, 'rb')):
            fp.write(line.replace('\r', ''))
        fp.close()